package com.lint.engine;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.id3.ID3v24Frames;
import org.jaudiotagger.tag.id3.ID3v24Tag;

public class LintMusic {

	private static final String WATCH_DIR = "C:\\Users\\Public\\Music\\Sample Music";

	private static final String OUTPUT_DIR = "C:\\Users\\Public\\Music\\";

	
	public LintMusic() {}

	/**
	 * @param args
	 */
	public static void run() {
		try {
			LintMusic app = new LintMusic();
			System.out.println("Process running ..");
			System.out.println("Directory to watch: " + WATCH_DIR + " ..");
			app.watchDirectory();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//Method to watch a specific directory for changes in the timestamp.
	public void watchDirectory() throws InterruptedException {
		File newFolder = new File(WATCH_DIR);
		List<String> fileList = new ArrayList<String>(Arrays.asList(newFolder.list()));
		List<String> tempFileList = null;
		long lastModified = newFolder.lastModified();
		System.out.println("Directory last modified: " + lastModified + " ..");
		String newAlbum = null;
		while (true) {
			if (newFolder.lastModified() > lastModified) {
				System.out.println("New folder added ..");
				tempFileList = new ArrayList<String>(Arrays.asList(newFolder.list()));
				tempFileList.removeAll(fileList);
				newAlbum = tempFileList.get(0);
				System.out.println("Folder name: " + newAlbum + " ..");
				//findMP3File(newAlbum);
				fileList = Arrays.asList(newFolder.list());
				tempFileList.clear();
				lastModified = newFolder.lastModified();
			}
			Thread.sleep(2000);
		}
	}

	//Inspects the new folder for any .mp3 files that it may contain.
	static public void findMP3File(String folderName) throws IOException {
		
		File targetFolder = new File(folderName);
		List<String> fileList = new ArrayList<String>(Arrays.asList(targetFolder.list()));
		Iterator<String> it = fileList.iterator();
		System.out.println("Locating music files ..");
		while ( it.hasNext()) {
			String musicFileName = it.next();
			File f = new File(folderName + "\\"+ musicFileName);
			if(f.isDirectory()) continue;
			InputStream is = new BufferedInputStream(new FileInputStream(folderName + "\\" + musicFileName));
			String mimeType = URLConnection.guessContentTypeFromStream(is);
			if(mimeType == null) continue;
			System.out.println(mimeType + " " + musicFileName);
//			int subStringStart = musicFileName.length() - 4;
//			if ( mp3String.substring(subStringStart, mp3String.length()).equals(".mp3") ) {
//				try {
//					System.out.println("Using: " + mp3String + " as input ..");
//					File mp3File = new File(pathToAlbum + "/" + mp3String);
//					System.out.println("Reading id3 tag ..");
//					readIDTag(mp3File);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//
//			} else {
//				System.out.println("No .mp3 files found, new folder is being ignored ..");
//				mp3String = null;
//			}
		}
	}

	//Reads the tag for any information I may need and stores in in an Mp3Data object.
	static public void readIDTag(File mp3) throws Exception {
		MP3File mp3File = (MP3File) AudioFileIO.read(mp3);
		Mp3Data audioData = new Mp3Data();

		if ( mp3File.hasID3v2Tag() ) {
			System.out.println("ID3v2 tag found ..");
			ID3v24Tag v24Tag = mp3File.getID3v2TagAsv24();
			audioData.setArtist(v24Tag.getFirst(ID3v24Frames.FRAME_ID_ARTIST));
			audioData.setAlbum(v24Tag.getFirst(ID3v24Frames.FRAME_ID_ALBUM));
			audioData.setYear(v24Tag.getFirst(ID3v24Frames.FRAME_ID_YEAR));

			System.out.println("Artist: " + audioData.getArtist() + " ..");
			System.out.println("Album: " + audioData.getAlbum() + " ..");
			System.out.println("Release year: " + audioData.getYear() + " ..");
		} else {
			System.out.println("No ID3 tag ..");
		}
		//createNewDirectories(audioData);
	}

	//Creates the new directory on the file system where the file will be moved. 
	//Structure example. ../music/Artist/Year - Album Name
//	public void createNewDirectories(Mp3Data audioData) throws IOException {
//		String artist = audioData.getArtist();
//		String album = audioData.getAlbum();
//		String year = audioData.getYear();
//
//		StringBuffer tempArtist = null;
//		if ( artist.startsWith("The") ) {
//			String[] artistArray = artist.split(" ");
//
//			for (int i = 1; i >= artistArray.length; ++i) {
//				tempArtist = tempArtist.append(artistArray[i]);
//			}
//			tempArtist.append(", The");
//			artist = tempArtist.toString();
//		}
//
//		File artistDirectory = new File(OUTPUT_DIR + "/" + artist);
//		File albumDirectory = new File(OUTPUT_DIR + "/" + artist + "/" + year + " - " + album);
//
//		if ( !artistDirectory.exists() ) {
//			System.out.println("Creating artist directory: " + OUTPUT_DIR + "/" + artist + " ..");
//			artistDirectory.mkdir();
//		} else {
//			System.out.println("Artist already exists: " + OUTPUT_DIR + "/" + artist + " ..");
//		}
//
//		if ( albumDirectory.exists() ) {
//			System.out.println("Artist and album already exist on system ..");
//			System.out.println(OUTPUT_DIR + "/" + artist + "/" + year + " - " + album);
//
//		} 
//		System.out.println("Creating album directory: " + OUTPUT_DIR + "/" + artist + "/" + year + " - " + album);
//		System.out.println("Copying files to new album directory ..");
//		copyAlbumFiles(this.albumInputFile, albumDirectory);
//	}

	//Copies all of the files within the album to its new location.
	public void copyAlbumFiles(File sourceLocation , File targetLocation)
	throws IOException {

		if ( sourceLocation.isDirectory() ) {
			if ( !targetLocation.exists() ) {
				targetLocation.mkdir();
			}

			String[] children = sourceLocation.list();
			for (int i=0; i<children.length; i++) {
				copyAlbumFiles(new File(sourceLocation, children[i]),
						new File(targetLocation, children[i]));
			}
		} else {

			InputStream in = new FileInputStream(sourceLocation);
			OutputStream out = new FileOutputStream(targetLocation);

			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
		}
	}
}